package com.scs.application.modules.fsd.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scs.application.modules.fsd.dto.SaveStockDTO;
import com.scs.application.modules.fsd.service.NJGLService;
import com.scs.application.modules.fsd.vo.LoginResult;
import com.scs.application.modules.fsd.vo.SPDResponseResult;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 南京鼓楼医院
 */
@RestController
@RequestMapping("/NJGL")
public class NJGLController extends IntelliAPIController {

    @Autowired
    private NJGLService njglService;
//    @PostMapping(value = "/config/spd")
    public RestResult netTest() {
        return RestResult.ok("网络连接正常--NJGL");
    }

    /**
     * 查询本仓间所有配送单
     *
     * @return
     */
    @GetMapping(value = "/outboundOrder/getOutboundOrder")
    public RestResult getOutboundOrder(@RequestHeader("Intellic-configuration") String config) {
        return RestResult.ok("获取配送单成功", njglService.getOutboundOrder(JSONObject.parseObject(config).getString("stationCode")));
    }

    /**
     * 获取配送单详情
     *
     * @return
     */
    @GetMapping(value = "/outboundOrder/getOrderDet")
    public RestResult getOrderDet(@RequestParam("orderId") String[] orderId) {
        List<String> orderIds = Arrays.asList(orderId);
        Map<String,Object> orderDet = new HashMap<>();
        orderDet.put("orderDet", njglService.getOrderDet(orderIds));
        return RestResult.ok("获取配送单详情成功",orderDet );
    }

    /**
     * 获取配送单详情(原始数据)
     *
     * @return
     */
    @GetMapping(value = "/outboundOrder/getOrderItem")
    public RestResult getOrderItem(@RequestParam("orderId") String[] orderId) {
        List<String> orderIds = Arrays.asList(orderId);
//        Map<String,Object> orderDet = new HashMap<>();
//        orderDet.put("orderDet", njglService.getOrderItem(orderIds));
        return RestResult.ok("获取配送单详情成功",njglService.getOrderItem(orderIds));
    }

    /**
     * 入库确认
     * @return
     */
    @PostMapping(value = "/outboundOrder/submit")
    public RestResult submit(@RequestHeader("Intellic-configuration") String config, @RequestBody Map<String, String> map) {
        List<String> orderIds = Arrays.asList(map.get("data"));
        SPDResponseResult responseResult = njglService.submit(JSONObject.parseObject(config).getString("stationCode"), map.get("business"), orderIds);
        if (responseResult.getCode() == 0)
            return RestResult.ok(responseResult.getMsg());
        return RestResult.error(responseResult.getMsg());
    }
    /**
     * 出库确认
     * @return
     */
    @PostMapping(value = "/delivery/submit")
    public RestResult deliverySubmit(@RequestHeader("Intellic-configuration") String config, @RequestBody Map<String, String> map) {
        String rfids = map.get("data");
        SPDResponseResult responseResult = njglService.deliverySubmit(JSONObject.parseObject(config).getString("stationCode"), rfids);
        if (responseResult.getCode() == 0)
            return RestResult.ok("出库成功");
        return RestResult.error(responseResult.getMsg());
    }

    /**
     * 获取理论库存
     * @return
     */
    @GetMapping(value = "/delivery/getTheoryStock")
    public RestResult getTheoryStock(@RequestHeader("Intellic-configuration") String config) {
        return RestResult.ok("获取理论库存成功", njglService.getTheoryStock(JSONObject.parseObject(config).getString("stationCode")));
    }

    public RestResult getScanResult(@RequestHeader("Intellic-configuration") String config, @RequestBody Map<String, String> map) {
        List<SaveStockDTO> stockList = JSONArray.parseArray(map.get("data"), SaveStockDTO.class);
        if(stockList==null) return RestResult.error("未扫描到标签!");
        stockList.forEach(s -> {
            s.setRfid(s.getRfid().replaceAll("F+$",""));
        });
        switch (map.get("business")){
            case "OutboundOrder"://入库扫描
            case "Delivery"://出库扫描
            case "Inventory"://盘点扫描
            case "logoutScan"://退出登录扫描
                return RestResult.ok("扫描成功", njglService.getScanResult(JSONObject.parseObject(config).getString("stationCode"), map.get("business"), stockList));
        }
        return super.getScanResult(config,map);//其它扫描
    }

    public RestResult getMessageData(@RequestHeader("Intellic-configuration") String config) {
        return RestResult.ok("获取提示信息成功", njglService.getMessageData(JSONObject.parseObject(config).getString("stationCode")));
    }

    public RestResult getShortcutData(@RequestHeader("Intellic-configuration") String config) {
        return RestResult.ok("获取统计信息成功", njglService.getShortcutData(JSONObject.parseObject(config).getString("stationCode")));
    }

    public RestResult getUserInfoByToken(@RequestParam("token") String token) {
        LoginResult loginResult = njglService.getUserInfoByToken(token);
        return RestResult.ok("获取该用户所拥有的权限成功", loginResult);
    }
    public RestResult getStockData(@RequestHeader("Intellic-configuration") String config) {
        return RestResult.ok("库存查询成功", njglService.getStockData(JSONObject.parseObject(config).getString("stationCode")));
    }

}
